﻿using System.IO;
using System.Text;
using System.Threading.Tasks;
using HK.Core.Common;
using HK.Core.Common.Data;
using HK.Core.IO.Protocol.Output;
using HK.Core.Utils;

namespace HK.Core.IO.Output
{
    /// <summary>
    /// 文件I/O协议
    /// </summary>
    public abstract class FileIO : ClassExtension, IFileIO
    {

        /// <summary>
        /// I/O 模式
        /// </summary>
        public FileIoMode Mode { get; protected set; } = FileIoMode.None;

        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; protected set; } = null;

#region Import

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="iOnCompleted">导入完成回调</param>
        /// <returns>true:OK; false:NG;</returns>
        public bool Import(UtilsTask.TaskCompleted iOnCompleted)
        {
            if (string.IsNullOrEmpty(Path) || !File.Exists(Path)) return true;

            // 执行
            var flg = DoImport();
            iOnCompleted.Invoke(flg);
            return flg;
        }

        /// <summary>
        /// 执行导入
        /// </summary>
        /// <returns>true:OK; false:NG;</returns>
        protected abstract bool DoImport();

#endregion

#region Export

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="iOnCompleted">导入完成回调</param>
        /// <returns>true:OK; false:NG;</returns>
        public async Task<bool> Export(UtilsTask.TaskCompleted iOnCompleted = null)
        {
            // 删除
            if (!Delete()) return false;

            // 执行
            return await UtilsTask.RunTaskAsync(DoExport, iOnCompleted, $"{GetType().Name}-{Mode}");
        }
        
        /// <summary>
        /// 执行导出
        /// </summary>
        /// <returns>true:OK; false:NG;</returns>
        protected abstract bool DoExport();

#endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>true:OK; false:NG;</returns>
        public bool Delete()
        {
            if (string.IsNullOrEmpty(Path)) return true;
            // 若文件存在，则删除
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
            return !File.Exists(Path);
        }
        
    }
    
    /// <summary>
    /// 文件I/O协议
    /// </summary>
    /// <typeparam name="TData">数据类型</typeparam>
    public abstract class FileIO<TData> : FileIO, IFileIO<TData>
        where TData : JsonData
    {
        /// <summary>
        /// 数据
        /// </summary>
        public TData Data { get; protected set; } = default(TData);

        /// <summary>
        /// 字节编码
        /// <para>* 默认 : UTF-8</para>
        /// </summary>
        public Encoding Encoding { get; protected set; } = Encoding.UTF8;
        
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="iForce">强制标志位</param>
        /// <returns>true:OK; false:NG;</returns>
        public virtual bool Clear(bool iForce = false)
        {
            if (default(TData) == Data) return false;
            Data.Clear();

            if (iForce)
            {
                return Delete();
            }
            return true;
        }
    }
}